#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include 	<unistd.h>

#define		A_RESULT_PATH 		"/home/parisa/RING-PAXOS-JOURNAL/svn/RP-original/results/acceptor/"
#define		LP_RESULT_PATH 		"/home/parisa/RING-PAXOS-JOURNAL/svn/RP-original/results/lp/"
#define		SCRIPT_PATH		 	"/home/parisa/RING-PAXOS-JOURNAL/svn/RP-original/scripts/"
#define		CONFIG_PATH			"/home/parisa/RING-PAXOS-JOURNAL/svn/RP-original/example_config.cfg"

#define		STRING_SIZE			1024
#define		NUM_EXPERIMENTS		30

#define		INTERVAL_STEP		500
#define		SUBMIT_STEP			1

int first_acceptor_node_ip, first_learner_node_ip, num_learners, num_acceptors, different_submit_per_wake;

void make_path(char original[STRING_SIZE], int id, char * final, char file_name[STRING_SIZE]);
void validate_input_parameters(int argc, char *argv[]);
//------------------------------------------------------------------------------------------
int main(int argc, char *argv[]) {
	validate_input_parameters(argc, argv);

	int folder_id;
	char lp_script[STRING_SIZE], a_script[STRING_SIZE], a_kill_script[STRING_SIZE], lp_kill_script[STRING_SIZE];

	int submit_per_wake, interval, packet_size;

	submit_per_wake = 14;
	interval = 1000;
	folder_id = 4;
	packet_size = 8622;

	strcpy(a_kill_script, SCRIPT_PATH);
	sprintf(a_kill_script, "%sa_kill.sh %d %d", SCRIPT_PATH, first_acceptor_node_ip, num_acceptors);

	strcpy(lp_kill_script, SCRIPT_PATH);
	sprintf(lp_kill_script, "%slp_kill.sh %d %d", SCRIPT_PATH, first_learner_node_ip, num_learners);

	//	while (submit_per_wake < 20)
	{
		//				double thr = submit_per_wake * packet_size;
		//				thr = thr / (double) interval;
		//				thr = thr * 8;
		//				thr = thr / (1024 * 1024);
		//				thr = thr * 1000 * 1000;

		//				if (thr > 250 || thr < 120) {
		//					printf("folder_id: %d 	num_proposers: %d  diff_submit_per_wake %d	packets: %d interval: %d ", folder_id, j, k, total_num_packets, interval);
		//					printf("expected sending throughput  %f\n", thr);
		//					break;
		//				}

		memset(a_script, '\0', STRING_SIZE);
		memset(lp_script, '\0', STRING_SIZE);

		sprintf(a_script, "%sacceptors.sh %d %d %s %s %d %d %d", SCRIPT_PATH, first_acceptor_node_ip, num_acceptors, CONFIG_PATH, A_RESULT_PATH, 1000, 20, folder_id);
		sprintf(lp_script, "%slp.sh %d %d %s %s %d %d %d", SCRIPT_PATH, first_learner_node_ip, num_learners, CONFIG_PATH, LP_RESULT_PATH, interval, submit_per_wake, folder_id);

		system(a_script);
		system(lp_script);

		sleep(156);

		system(a_kill_script);
		system(lp_kill_script);

		folder_id++;

		submit_per_wake++;
	}

	return 1;
}
//------------------------------------------------------------------------------------------
void make_path(char original[STRING_SIZE], int id, char *final, char file_name[STRING_SIZE]) {
	strcpy(final, original);
	char id_string[STRING_SIZE];

	sprintf(id_string, "%d", id);
	strcat(final, id_string);
	strcat(final, "/");
	strcat(final, file_name);
}
//------------------------------------------------------------------------------------------
void validate_input_parameters(int argc, char *argv[]) {
	if (argc != 5) {

		printf("Incompatible call. Please try again:\n"
			"<1. IP of first Acceptor.>\n"
			"<2. Number of acceptors.>\n"
			"<3. IP of first Learner.>\n"
			"<4. Number of Learners.>\n");

		exit(-1);

	} else {

		first_acceptor_node_ip = atoi(argv[1]);
		num_acceptors = atoi(argv[2]);

		first_learner_node_ip = atoi(argv[3]);
		num_learners = atoi(argv[4]);

	}
}

